package de.smarthome.message;

import java.io.IOException;

import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;
import de.smarthome.controllable.LPBlind;
import de.smarthome.controllable.LPCurtain;
import de.smarthome.controllable.LPLight;
import de.smarthome.controllable.LPWindow;

public class Connector {

	private static final String ip = "172.16.0.200";
	private static final String port = "12349";
	
	private static final String lightControl		= "LP.LIGHTCONTROL";
	private static final String curtainControl	= "LP.LIGHTCONTROL";
	private static final String blindControl		= "LP.LIGHTCONTROL";
	private static final String windowControl		= "WINDOW.CONTROL";

	public static String getIp() {
		return ip;
	}

	public static String getPort() {
		return port;
	}

	/*
	 * ----------LIGHT----------
	 */

	public void lightControl(JSONObject jsonO) {
		MessageJSN msg = new MessageJSN();
		setMessage(jsonO, msg, lightControl);
		statusChanged(msg);
	}
	
	
	/*
	 * ----------COLOR----------
	 */
	public void lightColorControl(JSONObject jsonO) {
		MessageJSN msg = new MessageJSN();
		setMessage(jsonO, msg, lightControl);
		statusChanged(msg);
	}

	/*
	 * ----------CURTAIN----------
	 */

	public void curtainControl(JSONObject jsonO) {
		MessageJSN msg = new MessageJSN();
		setMessage(jsonO, msg, curtainControl);
		statusChanged(msg);
	}


	/*
	 * ----------BLIND----------
	 */

	
	public void blindControl(JSONObject jsonO){
		MessageJSN msg = new MessageJSN();
		setMessage(jsonO, msg, blindControl);
		statusChanged(msg);
	}
	
	
	/*
	 * ----------WINDOW----------
	 */

	public void windowControl(JSONObject jsonO){
		MessageJSN msg = new MessageJSN();
		setMessage(jsonO, msg, windowControl);
		statusChanged(msg);
	}

	

	public void statusChanged(MessageJSN msg) {

		String[] params = new String[5];
		params[0] = getIp();
		params[1] = getPort();
		params[2] = msg.getTopic();
		params[3] = msg.getTyp();
		params[4] = msg.getMessage().toString();

		sendMessageToProxy send = new sendMessageToProxy();

		send.execute(params);
	}

	private static MessageJSN setMessage(JSONObject jO, MessageJSN msg, String control) {
		msg.setTopic(control);
		msg.setTyp("Topic");
		msg.setMessage(jO.toString());

		return msg;
	}

	private class sendMessageToProxy extends AsyncTask<String, Void, String> {
		//
		// params[0] Server; [1] Port; [2] TopicName; [3]topic/queue;
		@Override
		protected String doInBackground(String... params) {
			try {
				// new AndroidPublisher(adress; port; topic-name)
				AndroidPublisher publisher = new AndroidPublisher(params[0],
						Integer.valueOf(params[1]), params[2]);

				publisher.setMessage(params[4]);

				if (params[3].equals("Topic")) {
					Log.v("TEAM_AWESOME", "topic");
					publisher.publishToTopic();

				} else {
					Log.v("TEAM_AWESOME", "queue");
					publisher.publishToQueue();

				}
			} catch (IOException e) {
				Log.e("Publisher", "Can't publish!");
			}
			return null;
		}

	}

}
